var data = [
  { code: "biz", iconskip: "eova-icon877", id: 3, link: "", name: "综合业务", open: true, parent_id: 0, type: "dir" },
  {
    code: "sys",
    iconskip: "eova-icon294",
    id: 900,
    link: "",
    name: "系统管理",
    open: false,
    parent_id: 0,
    type: "dir"
  },
  { code: "eova", iconskip: "eova-icon169", id: 1, link: "", name: "平台维护", open: false, parent_id: 0, type: "dir" },
  {
    code: "eova_dict",
    iconskip: "eova-icon145",
    id: 24,
    link: "/single_grid/list/eova_dict",
    name: "字典管理",
    open: true,
    parent_id: 1,
    type: "single_grid"
  },
  {
    code: "eova_druid",
    iconskip: "eova-icon473",
    id: 1061,
    link: "/druid",
    name: "性能监控",
    open: true,
    parent_id: 1,
    type: "diy"
  },
  {
    code: "eova_menu",
    iconskip: "eova-icon43",
    id: 20,
    link: "/single_tree/list/eova_menu",
    name: "菜单管理",
    open: true,
    parent_id: 1,
    type: "single_tree"
  },
  {
    code: "eova_button",
    iconskip: "eova-icon169",
    id: 21,
    link: "/tree_grid/list/eova_button",
    name: "按钮管理",
    open: true,
    parent_id: 1,
    type: "tree_grid"
  },
  {
    code: "eova_object",
    iconskip: "eova-icon395",
    id: 22,
    link: "/master_slave_grid/list/eova_object",
    name: "元数据管理",
    open: true,
    parent_id: 1,
    type: "master_slave_grid"
  },
  {
    code: "eova_task",
    iconskip: "eova-icon280",
    id: 25,
    link: "/single_grid/list/eova_task",
    name: "定时调度",
    open: true,
    parent_id: 1,
    type: "single_grid"
  },
  {
    code: "eova_code",
    iconskip: "eova-icon157",
    id: 26,
    link: "/code",
    name: "神器·宝箱",
    open: true,
    parent_id: 1,
    type: "diy"
  },
  {
    code: "testtest",
    iconskip: "eova-icon171",
    id: 1062,
    link: "/single_grid/list/testtest",
    name: "测试测试",
    open: true,
    parent_id: 3,
    type: "single_grid"
  },
  {
    code: "lang",
    iconskip: "eova-icon0",
    id: 1063,
    link: "/single_grid/list/lang",
    name: "通用语言",
    open: true,
    parent_id: 3,
    type: "single_grid"
  },
  {
    code: "biz_demo",
    iconskip: "eova-icon145",
    id: 1000,
    link: "",
    name: "功能演示",
    open: true,
    parent_id: 3,
    type: "dir"
  },
  {
    code: "biz_demo_eova_all",
    iconskip: "eova-icon11",
    id: 1029,
    link: "/single_grid/list/biz_demo_eova_all",
    name: "EOVA控件",
    open: true,
    parent_id: 3,
    type: "single_grid"
  },
  {
    code: "sys_users",
    iconskip: "eova-icon518",
    id: 901,
    link: "/single_grid/list/sys_users",
    name: "用户管理",
    open: true,
    parent_id: 900,
    type: "single_grid"
  },
  {
    code: "sys_auth_role",
    iconskip: "eova-icon525",
    id: 902,
    link: "/single_grid/list/sys_auth_role",
    name: "角色管理",
    open: true,
    parent_id: 900,
    type: "single_grid"
  },
  {
    code: "sys_log",
    iconskip: "eova-icon1058",
    id: 903,
    link: "/single_grid/list/sys_log",
    name: "系统日志",
    open: true,
    parent_id: 900,
    type: "single_grid"
  },
  {
    code: "test01",
    iconskip: "eova-icon1",
    id: 1060,
    link: "/single_grid/list/test01",
    name: "UUID和远程校验",
    open: true,
    parent_id: 1000,
    type: "single_grid"
  },
  {
    code: "biz_demo_users",
    iconskip: "eova-icon49",
    id: 1001,
    link: "/single_grid/list/biz_demo_users",
    name: "单表CRUD",
    open: true,
    parent_id: 1000,
    type: "single_grid"
  },
  {
    code: "biz_demo_userscell",
    iconskip: "eova-icon294",
    id: 1005,
    link: "/single_grid/list/biz_demo_userscell",
    name: "表格单元格编辑",
    open: true,
    parent_id: 1000,
    type: "single_grid"
  },
  {
    code: "biz_demo_import",
    iconskip: "eova-icon84",
    id: 1006,
    link: "/single_grid/list/biz_demo_import",
    name: "导入导出",
    open: true,
    parent_id: 1000,
    type: "single_grid"
  },
  {
    code: "biz_users_exp",
    iconskip: "eova-icon22",
    id: 1007,
    link: "/single_grid/list/biz_users_exp",
    name: "非自增主键拓展表",
    open: true,
    parent_id: 1000,
    type: "single_grid"
  },
  {
    code: "biz_product",
    iconskip: "eova-icon160",
    id: 1009,
    link: "/single_grid/list/biz_product",
    name: "产品管理",
    open: true,
    parent_id: 1000,
    type: "single_grid"
  },
  {
    code: "biz_demo_hotel",
    iconskip: "eova-icon182",
    id: 1053,
    link: "/single_grid/list/biz_demo_hotel",
    name: "酒店管理(关联)",
    open: true,
    parent_id: 1000,
    type: "single_grid"
  },
  {
    code: "biz_demo_hotel_stock",
    iconskip: "eova-icon182",
    id: 1054,
    link: "/single_grid/list/biz_demo_hotel_stock",
    name: "酒店库存管理",
    open: true,
    parent_id: 1000,
    type: "single_grid"
  },
  {
    code: "biz_hotelzz",
    iconskip: "eova-icon182",
    id: 1010,
    link: "/master_slave_grid/list/biz_hotelzz",
    name: "酒店管理(主子)",
    open: true,
    parent_id: 1000,
    type: "master_slave_grid"
  },
  {
    code: "biz_demo_links",
    iconskip: "eova-icon33",
    id: 1035,
    link: "/single_grid/list/biz_demo_links",
    name: "友情链接管理",
    open: true,
    parent_id: 1000,
    type: "single_grid"
  },
  {
    code: "biz_demo_area",
    iconskip: "eova-icon43",
    id: 1048,
    link: "/single_tree/list/biz_demo_area",
    name: "地区树管理",
    open: true,
    parent_id: 1000,
    type: "single_tree"
  },
  {
    code: "biz_demo_tree_code",
    iconskip: "eova-icon43",
    id: 1049,
    link: "/single_tree/list/biz_demo_tree_code",
    name: "父字符串树",
    open: true,
    parent_id: 1000,
    type: "single_tree"
  },
  {
    code: "biz_demo_sale",
    iconskip: "eova-icon129",
    id: 1050,
    link: "/tree_grid/list/biz_demo_sale",
    name: "全国销售数据",
    open: true,
    parent_id: 1000,
    type: "tree_grid"
  },
  {
    code: "biz_caidan",
    iconskip: "eova-icon271",
    id: 1011,
    link: "",
    name: "报表管理",
    open: true,
    parent_id: 1000,
    type: "dir"
  },
  {
    code: "biz_demo_views",
    iconskip: "eova-icon21",
    id: 1046,
    link: "",
    name: "多表视图",
    open: false,
    parent_id: 1000,
    type: "dir"
  },
  {
    code: "biz_demo_money",
    iconskip: "eova-icon271",
    id: 1040,
    link: "/single_chart/list/biz_demo_money",
    name: "销售额报表",
    open: true,
    parent_id: 1011,
    type: "single_chart"
  },
  {
    code: "biz_data_login",
    iconskip: "eova-icon271",
    id: 1015,
    link: "/single_chart/list/biz_data_login",
    name: "每日登录用户数",
    open: true,
    parent_id: 1011,
    type: "single_chart"
  },
  {
    code: "biz_office_xls1",
    iconskip: "eova-icon779",
    id: 1056,
    link: "/office/list/biz_office_xls1",
    name: "高新园评分表",
    open: true,
    parent_id: 1011,
    type: "office"
  },
  {
    code: "biz_office_doc1",
    iconskip: "eova-icon820",
    id: 1057,
    link: "/office/list/biz_office_doc1",
    name: "高新园检查表",
    open: true,
    parent_id: 1011,
    type: "office"
  },
  {
    code: "biz_demo_views_orders",
    iconskip: "eova-icon49",
    id: 1044,
    link: "/single_grid/list/biz_demo_views_orders",
    name: "订单管理",
    open: true,
    parent_id: 1046,
    type: "single_grid"
  },
  {
    code: "biz_demo_views_users",
    iconskip: "eova-icon1121",
    id: 1047,
    link: "/single_grid/list/biz_demo_views_users",
    name: "多表用户信息",
    open: true,
    parent_id: 1046,
    type: "single_grid"
  }
]

// 可自定义pid的名字
var config = {
  pid: "parent_id"
}

function toTree(data, config) {
  // 删除 所有 children,以防止多次调用
  data.forEach(function(item) {
    delete item.children
  })

  // 将数据存储为 以 id 为 KEY 的 map 索引数据列
  var map = {}
  data.forEach(function(item) {
    map[item.id] = item
  })
  //        console.log(map);
  var val = []
  data.forEach(function(item) {
    // 以当前遍历项，的pid,去map对象中找到索引的id
    var parent = map[item[config.pid]]
    // 好绕啊，如果找到索引，那么说明此项不在顶级当中,那么需要把此项添加到，他对应的父级中
    if (parent) {
      ;(parent.children || (parent.children = [])).push(item)
    } else {
      //如果没有在map中找到对应的索引ID,那么直接把 当前的item添加到 val结果集中，作为顶级
      val.push(item)
    }
  })
  return val
}

console.log(JSON.stringify(toTree(data, config), null, 2))
// TODO 生成目录layui，生成树表 https://gitee.com/whvse/treetable-lay js位置也要规划